package com.xiangyang.pos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiangyang.pos.R;
import com.xiangyang.pos.bean.HttpBean;
import com.xiangyang.pos.bean.MobilePayDataBean;
import com.xiangyang.pos.constant.AppConstant;
import com.xiangyang.pos.http.HttpClient;
import com.xiangyang.pos.http.RequestParameters;
import com.xiangyang.pos.logger.Logger;
import com.xiangyang.pos.utils.SharedUtils;

/**
 * Created by Administrator on 2017/8/21 0021.
 *
 * 订单付款
 *
 *@author YeDalin
 *
 */

public class OrderReceivablesActivity extends Activity {

    private TextView tvAllMoney;
    private ImageView ivConfirmReceivables;
    private ImageView ivGoBack;

    String payWay;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_receivables);
        initView();
        setMyClick();
    }


    private void initView() {
        tvAllMoney = findViewById(R.id.tv_all_money);
        ivConfirmReceivables = findViewById(R.id.iv_confirm_receivables);
        ivGoBack = findViewById(R.id.iv_go_back);


        String money = SharedUtils.getStringPrefs(this, "free", "");
        tvAllMoney.setText("￥" + money);

        payWay = SharedUtils.getStringPrefs(this, "pay_way", "");


    }


    private void setMyClick() {
        ivConfirmReceivables.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Logger.e("xxxx", "pay_way=  " + payWay);

                if (payWay.equals("Cash")) {
                    cashPayHttp();
                } else {
                    mobilePayHttp();
                }

            }
        });

        ivGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }


    //现金支付网络接口访问
    private RequestParameters getCashPayData() {

        String shopId = SharedUtils.getStringPrefs(this, "shopId", "");
        String content = SharedUtils.getStringPrefs(this, "orderCode", "");

        RequestParameters parameters = new RequestParameters();
        parameters.put("pay_way", "Cash");
        parameters.put("shopId", shopId);
        parameters.put("ordercode", content);

        return parameters;
    }

    private final String CASH_PAY_URL = AppConstant.POS_URL + "/Pay/An/cashpay";

    private void cashPayHttp() {

        String UserTOKEN=SharedUtils.getStringPrefs(OrderReceivablesActivity.this,"UserTOKEN","");
        final String MOBILE_PAY_URL = AppConstant.POS_URL + "/Serch/An/payAgain"+"/u="+UserTOKEN;

        HttpClient httpClient = new HttpClient();
        httpClient.post(MOBILE_PAY_URL, getCashPayData(), new HttpClient.OnResponseListener() {
            @Override
            public void onResponse(String result) {
                Logger.e("xxx", "CashPay=" + result);
                if (!result.equals("Error")) {
                    Gson gson = new Gson();
                    MobilePayDataBean bean = gson.fromJson(result, MobilePayDataBean.class);
                    if (bean != null) {
                        if (bean.getStatus() == 10000) {
                            Toast.makeText(OrderReceivablesActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                            String orderCode = SharedUtils.getStringPrefs(OrderReceivablesActivity.this, "orderCode", "");
                            SharedUtils.setStringPrefs(OrderReceivablesActivity.this,"out_trade_no",orderCode);
                            Intent intent = new Intent(OrderReceivablesActivity.this, DealDoneActivity.class);
                            startActivity(intent);
                        } else if (bean.getStatus() == 10003) {
                            Toast.makeText(OrderReceivablesActivity.this, "处理中，请等待", Toast.LENGTH_SHORT).show();
                            sureOrderCashPayHttp(bean);
                        } else {
                            Toast.makeText(OrderReceivablesActivity.this, "支付失败，请重新支付", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(OrderReceivablesActivity.this, OrderListActivity.class);
                            startActivity(intent);
                        }
                    }
                } else {
                    Toast.makeText(OrderReceivablesActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    //移动支付网络接口访问
    private RequestParameters getMobilePayData() {

        String pay_way = SharedUtils.getStringPrefs(this, "pay_way", "");
        String auth_code = SharedUtils.getStringPrefs(this, "payCode", "");
        String content = SharedUtils.getStringPrefs(this, "orderCode", "");

        RequestParameters parameters = new RequestParameters();
        parameters.put("pay_way", pay_way);
        parameters.put("ordercode", content);
        parameters.put("payCodeNumber", auth_code);

        return parameters;
    }


//    String UserTOKEN = SharedUtils.getStringPrefs(this, "UserTOKEN", "");


    private void mobilePayHttp() {

        String UserTOKEN=SharedUtils.getStringPrefs(OrderReceivablesActivity.this,"UserTOKEN","");
        final String MOBILE_PAY_URL = AppConstant.POS_URL + "/Serch/An/payAgain"+"/u="+UserTOKEN;

        HttpClient httpClient = new HttpClient();
        httpClient.post(MOBILE_PAY_URL, getMobilePayData(), new HttpClient.OnResponseListener() {
            @Override
            public void onResponse(String result) {
                Logger.e("xxx", "mobilePay=" + result);
                if (!result.equals("Error")) {
                    Gson gson = new Gson();
                    MobilePayDataBean bean = gson.fromJson(result, MobilePayDataBean.class);
                    if (bean != null) {
                        if (bean.getStatus() == 10000) {
                            Toast.makeText(OrderReceivablesActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                            String orderCode = SharedUtils.getStringPrefs(OrderReceivablesActivity.this, "orderCode", "");
                            SharedUtils.setStringPrefs(OrderReceivablesActivity.this,"out_trade_no",orderCode);
                            Intent intent = new Intent(OrderReceivablesActivity.this, DealDoneActivity.class);
                            startActivity(intent);
                        } else if (bean.getStatus() == 10003) {
                            Toast.makeText(OrderReceivablesActivity.this, "处理中，请等待", Toast.LENGTH_SHORT).show();
                            sureOrderPayHttp(bean);
                        } else {
                            Toast.makeText(OrderReceivablesActivity.this, "支付失败，请重新支付", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(OrderReceivablesActivity.this, OrderListActivity.class);
                            startActivity(intent);
                        }
                    }
                } else {
                    Toast.makeText(OrderReceivablesActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }


    //确认订单支付状态网络请求
    private RequestParameters getOrderPayData(MobilePayDataBean bean) {
        RequestParameters parameters = new RequestParameters();
        parameters.put("trade_no", bean.getOrder().getTrade_no());
        parameters.put("out_trade_no", bean.getOrder().getOut_trade_no());
        parameters.put("shopId", bean.getOrder().getShopid());
        parameters.put("mid", bean.getOrder().getMid());
        parameters.put("payment_total_free", bean.getOrder().getPayment_total_free());
        parameters.put("orderArr", bean.getOrder().getContent());
        return parameters;
    }

//    private final String WAIT_URL = AppConstant.POS_URL + "/Pay/An/getAlipayOrderStatus"+"/u="+UserTOKEN;

    private void sureOrderPayHttp(final MobilePayDataBean bean) {

        String UserTOKEN=SharedUtils.getStringPrefs(OrderReceivablesActivity.this,"UserTOKEN","");
        final String WAIT_URL = AppConstant.POS_URL + "/Pay/An/getAlipayOrderStatus"+"/u="+UserTOKEN;

        HttpClient httpClient = new HttpClient();
        httpClient.post(WAIT_URL, getOrderPayData(bean), new HttpClient.OnResponseListener() {
            @Override
            public void onResponse(String result) {
                Logger.e("xxx", "waitSurePay=" + result);
                if (!result.equals("Error")) {
                    Gson gson = new Gson();
                    HttpBean httpBean = gson.fromJson(result, HttpBean.class);
                    if (httpBean != null) {
                        if (httpBean.getStatus() == 10000) {
                            Toast.makeText(OrderReceivablesActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                            String orderCode = SharedUtils.getStringPrefs(OrderReceivablesActivity.this, "orderCode", "");
                            SharedUtils.setStringPrefs(OrderReceivablesActivity.this,"out_trade_no",orderCode);
                            Intent intent = new Intent(OrderReceivablesActivity.this, DealDoneActivity.class);
                            startActivity(intent);
                        } else if (httpBean.getStatus() == 10003) {
                            Toast.makeText(OrderReceivablesActivity.this, "处理中，请等待", Toast.LENGTH_SHORT).show();
                            sureOrderPayHttp(bean);
                        } else{
                            Toast.makeText(OrderReceivablesActivity.this, "支付失败，请重新支付", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(OrderReceivablesActivity.this, OrderListActivity.class);
                            startActivity(intent);
                        }
                    }
                } else {
                    Toast.makeText(OrderReceivablesActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private final String WAIT_CASH_URL = AppConstant.POS_URL + "/Serch/An/getCashOrderStatus";

    private void sureOrderCashPayHttp(final MobilePayDataBean bean) {
        HttpClient httpClient = new HttpClient();
        httpClient.post(WAIT_CASH_URL, getOrderPayData(bean), new HttpClient.OnResponseListener() {
            @Override
            public void onResponse(String result) {
                Logger.e("xxx", "waitSurePay=" + result);
                if (!result.equals("Error")) {
                    Gson gson = new Gson();
                    HttpBean httpBean = gson.fromJson(result, HttpBean.class);
                    if (httpBean != null) {
                        if (httpBean.getStatus() == 10000) {
                            Toast.makeText(OrderReceivablesActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(OrderReceivablesActivity.this, DealDoneActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(OrderReceivablesActivity.this, "支付失败，请重新支付", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(OrderReceivablesActivity.this, OrderListActivity.class);
                            startActivity(intent);
                        }
                    }
                } else {
                    Toast.makeText(OrderReceivablesActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
